home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / disk / misc / FormatZIP.lha / FormatZIP / Source / FormatZIP.c
C/C++ Source or Header  |  1998-04-11  |  8KB  |  279 lines

  1. /* FormatZIP V 0.1 - format a Iomega ZIP drive for PC
  2.    1998 by Alexander Feigl - freely distributable as long as
  3.    my name is not removed from the distributiojn               */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <time.h>
  9.  
  10. #include <exec/devices.h>
  11. #include <exec/io.h>
  12. #include <exec/ports.h>
  13. #include <exec/memory.h>
  14. #include <exec/semaphores.h>
  15.  
  16. #include <dos/exall.h>
  17.  
  18. #include <utility/tagitem.h>
  19.  
  20.  
  21. #include <clib/exec_protos.h>
  22. #include <clib/dos_protos.h>
  23.  
  24.  
  25.  
  26. struct MsgPort *IOPort;
  27. struct IOStdReq *IORequest;
  28.  
  29.  
  30. const unsigned char MasterBootRecord[]=
  31.  {
  32.   0xeb,0x2e,0x49,0x50,0x41,0x52,0x54,0x20,0x63,0x6f,0x64,0x65,0x20,0x30,0x30,0x39,
  33.   0x20,0x2d,0x20,0x49,0x6f,0x6d,0x65,0x67,0x61,0x20,0x43,0x6f,0x72,0x70,0x6f,0x72,
  34.   0x61,0x74,0x69,0x6f,0x6e,0x20,0x2d,0x20,0x31,0x31,0x2f,0x32,0x33,0x2f,0x39,0x30,
  35.   0xfa,0xfc,0x8c,0xc8,0x8e,0xd0,0xbc,0x00,0x7c,0x8e,0xd8,0x8e,0xc0,0xb9,0x00,0x02,
  36.   0xbf,0x00,0x7e,0xbe,0x00,0x7c,0xf3,0xa4,0xe9,0x00,0x02,0xfb,0xbd,0x00,0x7e,0x8b,
  37.   0xfd,0xbe,0xbe,0x01,0xb9,0x04,0x00,0x80,0x3a,0x80,0x74,0x0b,0x83,0xc6,0x10,0xe2,
  38.   0xf6,0x8b,0xb5,0xb2,0x01,0xeb,0x51,0x56,0x83,0xc6,0x10,0x49,0xe3,0x0b,0x80,0x3a,
  39.   0x80,0x75,0xf5,0x8b,0xb5,0xb0,0x01,0xeb,0x3f,0x5e,0x56,0x8a,0x12,0x8a,0x72,0x01,
  40.   0x8a,0x4a,0x02,0x8a,0x6a,0x03,0xbb,0x00,0x7c,0xbe,0x05,0x00,0x56,0xb8,0x01,0x02,
  41.   0xcd,0x13,0x73,0x0e,0x33,0xc0,0xcd,0x13,0x5e,0x4e,0x75,0xf0,0x8b,0xb5,0xb4,0x01,
  42.   0xeb,0x16,0x5e,0xbe,0xfe,0x7d,0x81,0x3c,0x55,0xaa,0x74,0x06,0x8b,0xb5,0xb6,0x01,
  43.   0xeb,0x06,0x5e,0x03,0xf5,0xe9,0x48,0xfd,0xe8,0x1b,0x00,0x8b,0xb5,0xb8,0x01,0xe8,
  44.   0x14,0x00,0xb4,0x00,0xcd,0x16,0x33,0xc0,0x8e,0xc0,0x26,0xc7,0x06,0x72,0x04,0x34,
  45.   0x12,0xea,0xf0,0xff,0x00,0xf0,0x03,0xf5,0xac,0x3c,0x00,0x74,0x0b,0x56,0xb4,0x0e,
  46.   0xbb,0x07,0x00,0xcd,0x10,0x5e,0xeb,0xf0,0xc3,0x49,0x6e,0x76,0x61,0x6c,0x69,0x64,
  47.   0x20,0x70,0x61,0x72,0x74,0x69,0x74,0x69,0x6f,0x6e,0x20,0x74,0x61,0x62,0x6c,0x65,
  48.  
  49.   0x00,0x44,0x69,0x73,0x6b,0x20,0x69,0x73,0x20,0x6e,0x6f,0x74,0x20,0x62,0x6f,0x6f,
  50.   0x74,0x61,0x62,0x6c,0x65,0x00,0x45,0x72,0x72,0x6f,0x72,0x20,0x6c,0x6f,0x61,0x64,
  51.   0x69,0x6e,0x67,0x20,0x6f,0x70,0x65,0x72,0x61,0x74,0x69,0x6e,0x67,0x20,0x73,0x79,
  52.   0x73,0x74,0x65,0x6d,0x00,0x4d,0x69,0x73,0x73,0x69,0x6e,0x67,0x20,0x6f,0x70,0x65,
  53.   0x72,0x61,0x74,0x69,0x6e,0x67,0x20,0x73,0x79,0x73,0x74,0x65,0x6d,0x00,0x0d,0x0a,
  54.   0x52,0x65,0x70,0x6c,0x61,0x63,0x65,0x20,0x61,0x6e,0x64,0x20,0x73,0x74,0x72,0x69,
  55.   0x6b,0x65,0x20,0x61,0x6e,0x79,0x20,0x6b,0x65,0x79,0x20,0x77,0x68,0x65,0x6e,0x20,
  56.   0x72,0x65,0x61,0x64,0x79,0x0d,0x0a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  57.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  58.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  59.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1a,0x06,
  60.   0xe9,0x00,0x01,0x01,0x16,0x01,0x35,0x01,0x4e,0x01,0xd5,0xb5,0xc7,0x79,0x00,0x00,
  61.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  62.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  63.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,
  64.   0x01,0x00,0x06,0x3f,0x20,0x5f,0x20,0x00,0x00,0x00,0xe0,0xff,0x02,0x00,0x55,0xaa
  65.  };
  66.  
  67. const unsigned char BootBlock[]={
  68.   0xeb,0x3c,0x90,0x5a,0x49,0x50,0x46,0x4d,0x54,0x20,0x20,0x00,0x02,0x08,0x01,0x00,
  69.   0x02,0x00,0x02,0x00,0x00,0xf8,0x60,0x00,0x20,0x00,0x40,0x00,0x20,0x00,0x00,0x00,
  70.   0xe0,0xff,0x02,0x00,0x00,0x00,0x29,0xbc,0x07,0x4d,0x37,0x5a,0x49,0x50,0x2d,0x31,
  71.   0x30,0x30,0x20,0x20,0x20,0x20,0x46,0x41,0x54,0x31,0x36,0x20,0x20,0x20,0xfb,0x31,
  72.   0xc0,0x8e,0xd0,0x8e,0xd8,0xbc,0x00,0x7c,0xbe,0x59,0x7c,0xb4,0x0e,0xbb,0x07,0x00,
  73.   0xac,0x84,0xc0,0x74,0xfe,0xcd,0x10,0xeb,0xf7,0x5a,0x49,0x50,0x46,0x6f,0x72,0x6d,
  74.   0x61,0x74,0x20,0x20,0x20,0x20,0x6e,0x6f,0x6e,0x2d,0x62,0x6f,0x6f,0x74,0x61,0x62,
  75.   0x6c,0x65,0x20,0x64,0x69,0x73,0x6b,0x21,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  76.  }; 
  77.  
  78.  
  79. int WriteBlock(unsigned long block,const unsigned char *blockptr)
  80.  {
  81.   IORequest->io_Command=CMD_WRITE;
  82.   IORequest->io_Flags=0;
  83.   IORequest->io_Length=512;
  84.   IORequest->io_Data=(APTR) blockptr;
  85.   IORequest->io_Offset=block<<9;
  86.   
  87.   DoIO(((struct IORequest *) IORequest));
  88.   if (IORequest->io_Error) return(1);
  89.   return(0);
  90.  }
  91.  
  92.  
  93. void Cleanup()
  94.  {
  95.   CloseDevice(((struct IORequest *) IORequest));
  96.   DeleteIORequest(IORequest);
  97.   DeleteMsgPort(IOPort);
  98.  
  99.  }
  100.  
  101. int main(int argc,char **argv)
  102.  {
  103.   unsigned char device[64];
  104.   unsigned long unit;
  105.  
  106.   printf("FormatZIP V0.1 - Format ZIPs for PC\n1998 by Alexander Feigl <sp7ock@dl-net.de>\n");
  107.  
  108.   if (argc!=3)
  109.    {
  110.     printf("Usage : %s <device> <unit>\n",argv[0]);
  111.     exit(20);
  112.    }
  113.   strcpy(device,argv[1]);
  114.   sscanf(argv[2],"%ld",&unit);
  115.   if ( (unit<0) || (unit>1024))
  116.    {
  117.     puts("Invalid parameters!");
  118.     exit(20);
  119.    }
  120.  
  121.   /* Open exec device for direct access to the ZIP drive */
  122.   
  123.   IOPort=CreateMsgPort();
  124.   if (IOPort==NULL)
  125.    {
  126.     puts("Cannot create msg port!");
  127.     exit(20);
  128.    }
  129.   IORequest=CreateIORequest(IOPort,sizeof(*IORequest));
  130.   if (IORequest==NULL)
  131.    {
  132.     DeleteMsgPort(IOPort);
  133.     puts("Cannot create IO Request!");
  134.     exit(20);
  135.    }
  136.   if (OpenDevice(device,unit,((struct IORequest *) IORequest),0))
  137.    {
  138.     DeleteIORequest(IORequest);
  139.     DeleteMsgPort(IOPort);
  140.     puts("Cannot open device driver!");
  141.     exit(20);
  142.    }
  143.  
  144.   /* Write Master Boot Record - I got it from a FujiFilm ZIP-100 disk
  145.      write first block from array - pad rest of MBR with 0x48          */
  146.  
  147.   puts("Writing Master Boot Record (MBR) ...");
  148.  
  149.   if (WriteBlock(0,MasterBootRecord))
  150.    {
  151.     puts("Cannot write MBR!");
  152.     Cleanup();
  153.     exit(20);
  154.    }
  155.    {  
  156.     unsigned char mbrpad[512];
  157.     unsigned long i;
  158.     for (i=0;i<512;++i) mbrpad[i]=0x48;
  159.     for (i=1;i<32;++i) 
  160.      {
  161.       if (WriteBlock(i,mbrpad))
  162.        {
  163.         puts("Cannot write MBR padding!");
  164.         Cleanup();
  165.         exit(20);
  166.        }
  167.      }
  168.    }
  169.  
  170.   Cleanup();
  171.   exit(20);
  172.  
  173.   
  174.   /* write ZIP bootblock - first part from array - pad with 0 
  175.      NOTE : the volume ID is initialized with random numbers so that
  176.             the filesystem can distinguish between different ZIPs     */
  177.  
  178.   puts("Writing boot record ...");
  179.    {
  180.     unsigned char boot[512];
  181.     unsigned long i;
  182.     
  183.     for (i=128;i<512;++i) boot[i]=0x0;
  184.  
  185.     for (i=0;i<128;++i) boot[i]=BootBlock[i];
  186.     boot[510]=0x55;
  187.     boot[511]=0xaa;
  188.  
  189.     /* randomize volume ID */
  190.  
  191.     srand(time(NULL));
  192.     boot[39]= (rand()%256);
  193.     boot[40]= (rand()%256);
  194.     boot[41]= (rand()%256);
  195.     boot[42]= (rand()%256);
  196.  
  197.     if (WriteBlock(32,boot))
  198.      {
  199.       puts("Cannot write bootblock!");
  200.       Cleanup();
  201.       exit(20);
  202.      }
  203.  
  204.    }
  205.  
  206.   /* write ZIP FAT - it is padded with 0-bytes - only the first four 
  207.      bytes of the both FATs have do be initalized differently        */
  208.  
  209.   puts("Wrinting File Allocation Table (FAT) ...");
  210.    {
  211.     unsigned char fat[512];
  212.     unsigned long i,j;
  213.     for (j=33;j<225;++j)
  214.      {
  215.       for (i=0;i<512;++i) fat[i]=0x0;
  216.       if ( (j==33) || (j==129))
  217.        {
  218.         fat[0]=0xf8;
  219.         fat[1]=0xff;
  220.         fat[2]=0xff;
  221.         fat[3]=0xff;
  222.        }
  223.  
  224.       if (WriteBlock(j,fat))
  225.        {
  226.         puts("Cannot write FAT!");
  227.         Cleanup();
  228.         exit(20);
  229.        }
  230.      }
  231.  
  232.    }
  233.  
  234.   /* write ZIP root directory - it's padded with 0-bytes except the first
  235.      directory entry which is the disk label                             */
  236.  
  237.   puts("Writing root directory ...");
  238.  
  239.    {
  240.     unsigned char dir[512];
  241.     unsigned long i,j;
  242.     for (j=225;j<257;++j)
  243.      {
  244.       for (i=0;i<512;++i) dir[i]=0x0;
  245.       if (j==225)
  246.        {
  247.         memcpy(dir,"ZIP-100    ",11);
  248.         dir[11]=0x28;
  249.         dir[22]=0xf4;
  250.         dir[23]=0x72;
  251.         dir[24]=0x7c;
  252.         dir[25]=0x24;
  253.  
  254.        }
  255.  
  256.       if (WriteBlock(j,dir))
  257.        {
  258.         puts("Cannot write root directory!");
  259.         Cleanup();
  260.         exit(20);
  261.        }
  262.      }
  263.  
  264.  
  265.    }
  266.  
  267.   /* Close the exec device */
  268.  
  269.   Cleanup();
  270.  
  271.   return(0);
  272.  }
  273.  
  274.  
  275. void __openliberror(void)
  276.  {
  277.   exit(20);
  278.  }
  279.